package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/customer"
	customerReq "counter-help/admin/server/model/customer/request"
)

type CusAuthService struct{}

// CreateCusAuth 创建cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) CreateCusAuth(cusAuth *customer.CusAuth) (err error) {

	err = global.GVA_DB.Create(cusAuth).Error
	return err
}

// DeleteCusAuth 删除cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) DeleteCusAuth(ID string) (err error) {
	err = global.GVA_DB.Delete(&customer.CusAuth{}, "id = ?", ID).Error
	return err
}

// DeleteCusAuthByIds 批量删除cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) DeleteCusAuthByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customer.CusAuth{}, "id in ?", IDs).Error
	return err
}

// UpdateCusAuth 更新cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) UpdateCusAuth(cusAuth customer.CusAuth) (err error) {
	err = global.GVA_DB.Model(&customer.CusAuth{}).Where("id = ?", cusAuth.ID).Updates(&cusAuth).Error
	return err
}

// GetCusAuth 根据ID获取cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) GetCusAuth(ID string) (cusAuth customer.CusAuth, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cusAuth).Error
	return
}

// GetCusAuthInfoList 分页获取cusAuth表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusAuthService *CusAuthService) GetCusAuthInfoList(info customerReq.CusAuthSearch) (list []customer.CusAuth, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&customer.CusAuth{})
	var cusAuths []customer.CusAuth
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&cusAuths).Error
	return cusAuths, total, err
}
